package com.coscon.test;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: liyzh-dbms-boot
 * @ClassName TestSqlClean
 * @description:
 * @author: liyzh
 * @create: 2023-05-31 10:51
 * @Version 1.0
 **/
public class 下载cctv视频 {
    static String targetDir = "f:\\2022年中秋晚会";
    static int totalVideoNum = 53;
    static String url_template = "https://vod.cntv.myhwcdn.cn/flash/mp4video63/TMS/2022/09/15/2842ce4711694807b0076c3681befd36_h2642000000nero_aac16-{N}.mp4";

    /**
     * 执行下载
     * @author liyzh
     * @date 2023/9/20 16:47
     */
    public static void execDownload() throws IOException {
        long s = System.currentTimeMillis();
        for(int  i =1; i<=totalVideoNum; i++ ){
            String fileName =  i + ".mp4";
            String target = targetDir + File.separator + fileName;
            File file = new File(target);
            String url = StringUtils.replace(url_template,"{N}",i+"");
            System.out.println(url);
            System.out.println(target);
            if(!file.isFile()){
                FileUtils.copyURLToFile(new URL(url),new File(target));
            }
        }
        long e = System.currentTimeMillis();
        System.out.println( "总时间：" + (e-s) );
    }

    public static void mergeVideo() throws IOException {
        String filename = "filelist.txt";
        String mergerFilePath = targetDir + File.separator + filename;
        File file = new File(mergerFilePath);

        List<String> list = new ArrayList<>();
        for(int  i =1; i<=totalVideoNum; i++ ){
            list.add("file '"+i+".mp4'");
        }
        FileUtils.writeLines(file,list);
        System.out.println("执行合并视频命令：");
        System.out.println("ffmpeg -f concat -safe 0 -i "+filename+" -c copy output.mp4");
    }



    public static void main(String[] args) throws Exception {
        execDownload();
        mergeVideo();
    }

}